﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UnitComponent.cs" company="Marcelo Roca">
//   Copyright (c) 2011 Marcelo Roca. All rights reserved.
// </copyright>
// <author name="Marcelo Roca" email="marce@marceloroca.com"/>
// <license>
//   This program is free software: you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
// 
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program.  If not, see http://www.gnu.org/licenses/.
// </license>
// --------------------------------------------------------------------------------------------------------------------

namespace GameKits.Unity.Rts.UnitComponents
{
    using UnityEngine;

    public abstract class UnitComponent
    {
        /// <summary>
        /// el healt del componente
        /// </summary>
        private float healt = 1;

        private bool isDirty;

        /// <summary>
        /// Gets or sets a value indicating whether Enabled.
        /// </summary>
        [SmartInspector.BrowsableField]
        public bool IsEnabled
        {
            get;
            set;
        }

        /// <summary>
        /// Gets a value indicating whether IsInitialized.
        /// </summary>
        public bool IsInitialized
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets or sets Healt.
        /// </summary>
        /// <remarks>
        /// El valor puede ser solo entre 0 y 1
        /// </remarks>
        [SmartInspector.BrowsableNumeric(Min = 0, Max = 1)]
        public float Healt
        {
            get { return this.healt; }
            set { this.healt = Mathf.Clamp01(value); }
        }

        /// <summary>
        /// Gets ParentUnit.
        /// </summary>
        public UnitBase ParentUnit
        {
            get;
            private set;
        }

        /// <summary>
        /// Actualizamos el componente
        /// </summary>
        /// <param name="deltaTime">
        /// The delta time.
        /// </param>
        public void Update(float deltaTime)
        {
            if (!this.IsEnabled)
            {
                return;
            }

            if (this.isDirty)
            {
                this.OnUpdateSettings();
                this.isDirty = false;
            }

            this.OnUpdate(deltaTime);
        }

        /// <summary>
        /// Muestra los gizmos en el editor
        /// </summary>
        public void ShowGizmos()
        {
            this.OnShowGizmos();
        }

        /// <summary>
        /// Realizamos la inicialización del componente
        /// </summary>
        /// <param name="parentUnit">
        /// The parent Unit.
        /// </param>
        public void Initialize(UnitBase parentUnit)
        {
            if (this.IsInitialized)
            {
                return;
            }

            this.ParentUnit = parentUnit;

            this.OnInitialize();

            this.IsInitialized = true;
        }

        /// <summary>
        /// Indicamos al componente que debemos actualizsar los settings
        /// </summary>
        protected void SetDirty()
        {
            this.isDirty = true;
        }

        /// <summary>
        /// Actualizamos el componente
        /// </summary>
        /// <param name="elapsedTime">
        /// The elapsed time.
        /// </param>
        protected virtual void OnUpdate(float elapsedTime)
        {
        }

        /// <summary>
        /// Muewsta los gizmos en el editor
        /// </summary>
        protected virtual void OnShowGizmos()
        {
        }

        /// <summary>
        /// Realizamos la inicialización del componente
        /// </summary>
        protected virtual void OnInitialize()
        {
        }

        protected virtual void OnUpdateSettings()
        {
        }
    }
}
